/**
 * 
 */
package main;

import java.util.ArrayList;
import java.util.List;

/**
 * 多看看一些数据字典的格式，
 * @author heantai
 * 2013-6-22 下午4:51:52
 * 
 */
public class Table {
	/**表格格式，英文+空格+中文*/
	public static final int NAME_PATTERN_EN_SPACE_ZH=1;

	//TODO 外键
	//主键，索引等
	private String pk;
	private List<Column> columns=new ArrayList<Column>();
	private String name;
	//专门的一行说明表的用处，而不是仅仅一个中文名
	private String comment;
	
	public void addColumn(Column column){//仅支持单个主键
		columns.add(column);
		if(column.isPk()){
			this.pk=column.getName();
		}
	}
	
	//comment on table t is '';
	//comment on column t.c is '';
	public String toSql4Oracle(){
		String seq=System.getProperty("line.separator");
		StringBuilder sql=new StringBuilder();
		StringBuilder comments=new StringBuilder();
		sql.append("create table ").append(name).append(" (").append(seq);
		for (int i = 0; i < columns.size(); i++) {
			Column c=columns.get(i);
			sql.append(c.getName()).append(" ").append(c.getType());
			if(!c.isNullable()){
				sql.append(" not null");
			}
			sql.append(",").append(seq);
			if(c.getComment()!=null){
				comments.append("comment on column ").append(name).append(".").append(c.getName())
					.append(" is '").append(c.getComment()).append("';").append(seq);
			}
		}
		if(pk!=null){
			sql.append("primary key (").append(pk).append(")");
		}else{
			sql.deleteCharAt(sql.length()-1);
		}
		sql.append(");").append(seq);
		return sql.toString()+comments.toString();
	}

	public final String getPk() {
		return pk;
	}

	public final void setPk(String pk) {
		this.pk = pk;
	}

	public final String getName() {
		return name;
	}

	public final void setName(String name) {
		this.name = name;
	}

	public final String getComment() {
		return comment;
	}

	public final void setComment(String comment) {
		this.comment = comment;
	}
}
